package com.yjd.bi.statistic.control;

import com.yjd.bi.comm.utils.BiDateUtil;
import com.yjd.bi.report.enums.TimeType;
import com.yjd.bi.report.model.EChartsModel;
import com.yjd.bi.user.service.IBasicDataAppService;
import com.yjd.bi.user.service.IBasicDataPcService;
import com.yjd.bi.user.service.IBasicDataWapService;
import com.yjd.comm.annotation.DateString;
import com.yjd.comm.annotation.ServiceRemoteProxy;
import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.enums.UserEnums;
import com.yjd.comm.rpm.RpmServiceKeyEnum;
import com.yjd.comm.util.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.Map;
/**
 * 平台数据  pc wap app基础数据统计控制器
 * @author zhoujin@yijiedai.com
 *
 */
@Controller
@Scope("prototype")
@RequestMapping("basicdata")
public class BasicDataStaitisticControl extends BaseControl{
	
	private static final String PC_RECH_CHANNEL = "'pc'";// 充值渠道:医界贷主站
	private static final String IOS_RECH_CHANNEL = "'ios'";// 充值渠道:ios
	private static final String ANDRIOD_RECH_CHANNEL = "'android'";// 充值渠道:android
	private static final String WAP_RECH_CHANNEL = "'wap'";// 充值渠道:医界贷wap微站
	
	@ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
	private IBasicDataPcService basicDataPcService;
	@ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
	private IBasicDataAppService basicDataAppService;
	@ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
	private IBasicDataWapService basicDataWapService;
	
	/**
	 * 用户信息统计页面 pc
	 * 
	 * @return
	 * @throws Exception
	 *             String
	 */
	@RequestMapping("/pc/index")
	public String pcindex() throws Exception {
        request.setAttribute("start", "2013-12-21");
        request.setAttribute("end", BiDateUtil.initBiDate());
        return "basic.pc.index";
	}
	/**
	 * 用户信息统计页面 app
     *
     * @return
	 * @throws Exception
	 *             String
	 */
	@RequestMapping("/app/index")
	public String appindex() throws Exception {
        request.setAttribute("start", "2013-12-21");
        request.setAttribute("end", BiDateUtil.initBiDate());
        return "basic.app.index";
	}
	/**
	 * 用户信息统计页面 wap
     *
     * @return
	 * @throws Exception
	 *             String
	 */
	@RequestMapping("/wap/index")
	public String wapindex() throws Exception {
        request.setAttribute("start", "2013-12-21");
        request.setAttribute("end", BiDateUtil.initBiDate());
        return "basic.wap.index";
	}
	
	
	/**
	 * pc总览
	 * 
	 * @throws Exception
	 *             void
	 */
	@RequestMapping("/pc/summary")
	public void pcsummary(@DateString String start, @DateString String end)
			throws Exception {
		Map<String, Object> paramMap = ServletUtil.getParameterMap(request);
		paramMap.put("start", FrameUtil.getTime(DateUtil.parseDate(start,
				DateUtil.PATTERN_yyyyMMdd)));
		paramMap.put("end", FrameUtil.getTime(DateUtil.parseDate(end,
				DateUtil.PATTERN_yyyyMMdd))+86399);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 注册用户数
		resultMap.put("register_account",basicDataPcService.getFundsSum("register_amount", start, end));
		// 首次充值
		resultMap.put("first_recharge",basicDataPcService.getFundsSum("first_rech_amount", start, end));
		// 首次投资
		resultMap.put("first_invest",basicDataPcService.getFundsSum("first_invest_amount", start, end));
		// 首次充值(不区分客户端)
		resultMap.put("first_recharge_all",basicDataPcService.getFundsSum("first_rech_all", start, end));
		// 首次投资(不区分客户端)
		resultMap.put("first_invest_all",basicDataPcService.getFundsSum("first_invest_all", start, end));
		// 充值金额
		resultMap.put("rech_price",NumberUtil.formatNumber(basicDataPcService.getFundsSum("rech_price", start, end), NumberUtil.FORMAT_4));
		// 投资金额
		resultMap.put("invest_price",NumberUtil.formatNumber(basicDataPcService.getFundsSum("invest_price", start, end), NumberUtil.FORMAT_4));
		// 投资项目金额
		resultMap.put("borrow_price",NumberUtil.formatNumber(basicDataPcService.getFundsSum("borrow_price", start, end), NumberUtil.FORMAT_4));
		// 债权转让金额
		resultMap.put("creditor_transfer_price",NumberUtil.formatNumber(basicDataPcService.getFundsSum("creditor_transfer_price", start, end), NumberUtil.FORMAT_4));
		
		
		// 投资人数
		paramMap.put("channel", 0+","+UserEnums.Channel.PC.getCode());
		paramMap.put("propertyName", "invest_amount");
		resultMap.put("invest_amount",basicDataPcService.getSummary(paramMap));
		// 登录用户数
		paramMap.put("propertyName", "login_amount");
		resultMap.put("login_amount",basicDataPcService.getSummary(paramMap));
		
		// 充值人数
		paramMap.put("rech_channel","1,5,6");
		paramMap.put("propertyName", "rech_amount");
		resultMap.put("rech_amount",basicDataPcService.getSummary(paramMap));
		
		super.successData(resultMap);
	}
	
	/**
	 * app总览
	 * 
	 * @throws Exception
	 *             void
	 */
	@RequestMapping("/app/summary")
	public void appsummary(@DateString String start, @DateString String end ,@RequestParam(required = false) Integer type)
			throws Exception {
		Map<String, Object> paramMap = ServletUtil.getParameterMap(request);
		paramMap.put("start", FrameUtil.getTime(DateUtil.parseDate(start,
				DateUtil.PATTERN_yyyyMMdd)));
		paramMap.put("end", FrameUtil.getTime(DateUtil.parseDate(end,
				DateUtil.PATTERN_yyyyMMdd))+86399);
		
		String optionType = "2,3";
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 注册用户数
		resultMap.put("register_account",basicDataAppService.getFundsSum("register_amount", start, end, optionType));
		// 首次充值
		resultMap.put("first_recharge",basicDataAppService.getFundsSum("first_rech_amount", start, end, optionType));
		// 首次投资
		resultMap.put("first_invest",basicDataAppService.getFundsSum("first_invest_amount", start, end, optionType));
		// 首次充值(不区分客户端)
		resultMap.put("first_recharge_all",basicDataAppService.getFundsSum("first_rech_all", start, end, optionType));
		// 首次投资(不区分客户端)
		resultMap.put("first_invest_all",basicDataAppService.getFundsSum("first_invest_all", start, end, optionType));
		// 充值金额
		resultMap.put("rech_price",NumberUtil.formatNumber(basicDataAppService.getFundsSum("rech_price", start, end, optionType), NumberUtil.FORMAT_4));
		// 投资金额
		resultMap.put("invest_price",NumberUtil.formatNumber(basicDataAppService.getFundsSum("invest_price", start, end, optionType), NumberUtil.FORMAT_4));
		// 投资项目金额
		resultMap.put("borrow_price",NumberUtil.formatNumber(basicDataAppService.getFundsSum("borrow_price", start, end, optionType), NumberUtil.FORMAT_4));
		// 债权转让金额
		resultMap.put("creditor_transfer_price",NumberUtil.formatNumber(basicDataAppService.getFundsSum("creditor_transfer_price", start, end, optionType), NumberUtil.FORMAT_4));
		
		// 投资人数
		paramMap.put("channel", optionType);
		paramMap.put("propertyName", "invest_amount");
		resultMap.put("invest_amount",basicDataPcService.getSummary(paramMap));
		// 登录用户数
		paramMap.put("propertyName", "login_amount");
		resultMap.put("login_amount",basicDataPcService.getSummary(paramMap));
		
		// 充值人数
		paramMap.put("rech_channel","2,3,8,9");
		paramMap.put("propertyName", "rech_amount");
		resultMap.put("rech_amount",basicDataPcService.getSummary(paramMap));
		
		super.successData(resultMap);
	}
	
	/**
	 * wap总览
	 * 
	 * @throws Exception
	 *             void
	 */
	@RequestMapping("/wap/summary")
	public void wapsummary(@DateString String start, @DateString String end,@RequestParam(required = false) Integer type)
			throws Exception {
		Map<String, Object> paramMap = ServletUtil.getParameterMap(request);
		paramMap.put("start", FrameUtil.getTime(DateUtil.parseDate(start,
				DateUtil.PATTERN_yyyyMMdd)));
		paramMap.put("end", FrameUtil.getTime(DateUtil.parseDate(end,
				DateUtil.PATTERN_yyyyMMdd))+86399);
		
		String optionType ="";
		if(type ==null ){
			optionType= "4,5";
		}else{
			optionType = type.toString();
		}
		
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 注册用户数
		resultMap.put("register_account",basicDataWapService.getFundsSum("register_amount", start, end, optionType));
		// 首次充值
		resultMap.put("first_recharge",basicDataWapService.getFundsSum("first_rech_amount", start, end, optionType));
		// 首次投资
		resultMap.put("first_invest",basicDataWapService.getFundsSum("first_invest_amount", start, end, optionType));
		// 首次充值
		resultMap.put("first_recharge_all",basicDataWapService.getFundsSum("first_rech_all", start, end, optionType));
		// 首次投资
		resultMap.put("first_invest_all",basicDataWapService.getFundsSum("first_invest_all", start, end, optionType));
		// 充值金额
		resultMap.put("rech_price",type==null?NumberUtil.formatNumber(ArithUtil.div(basicDataWapService.getFundsSum("rech_price", start, end, optionType), 2), NumberUtil.FORMAT_4):NumberUtil.formatNumber(basicDataWapService.getFundsSum("rech_price", start, end, optionType), NumberUtil.FORMAT_4));
		// 投资金额
		resultMap.put("invest_price",NumberUtil.formatNumber(basicDataWapService.getFundsSum("invest_price", start, end, optionType), NumberUtil.FORMAT_4));
		// 投资项目金额
		resultMap.put("borrow_price",NumberUtil.formatNumber(basicDataWapService.getFundsSum("borrow_price", start, end, optionType), NumberUtil.FORMAT_4));
		// 债权转让金额
		resultMap.put("creditor_transfer_price",NumberUtil.formatNumber(basicDataWapService.getFundsSum("creditor_transfer_price", start, end, optionType), NumberUtil.FORMAT_4));
		
		// 投资人数
		paramMap.put("channel", optionType);
		paramMap.put("propertyName", "invest_amount");
		resultMap.put("invest_amount",basicDataPcService.getSummary(paramMap));
		// 登录用户数
		paramMap.put("propertyName", "login_amount");
		resultMap.put("login_amount",basicDataPcService.getSummary(paramMap));
				
		// 充值人数
		paramMap.put("rech_channel","4,7");
		paramMap.put("propertyName", "rech_amount");
		resultMap.put("rech_amount",basicDataPcService.getSummary(paramMap));
		super.successData(resultMap);
	}
	
	/**
	 * 获取平台pc的基本数据
	 * @param timeType
	 * @param timeFrom
	 * @param field
	 * @throws Exception
	 */
	@RequestMapping("/getpcdata")
	public void getpcdata(@RequestParam(required = false) Integer timeType,
			@DateString String timeFrom,
			@RequestParam(required = false) String field) throws Exception {
		EChartsModel echartsMoel = basicDataPcService.getDates(
				TimeType.getType(timeType), Long.valueOf(timeFrom), field);
		super.successData(FrameUtil.newHashMap("option", echartsMoel));
	}
	
	/**
	 * 获取平台app的基本数据
	 * @param timeType
	 * @param timeFrom
	 * @param field
	 * @throws Exception
	 */
	@RequestMapping("/getappdata")
	public void getappdata(@RequestParam(required = false) Integer timeType,@RequestParam(required = false) Integer type,
			@DateString String timeFrom,
			@RequestParam(required = false) String field) throws Exception {
		EChartsModel echartsMoel = basicDataAppService.getDates(
				type,TimeType.getType(timeType), Long.valueOf(timeFrom), field);
		super.successData(FrameUtil.newHashMap("option", echartsMoel));
	}
	
	/**
	 * 获取平台wap的基本数据
	 * @param timeType
	 * @param timeFrom
	 * @param field
	 * @throws Exception
	 */
	@RequestMapping("/getwapdata")
	public void getwapdata(@RequestParam(required = false) Integer timeType,@RequestParam(required = false) Integer type,
			@DateString String timeFrom,
			@RequestParam(required = false) String field) throws Exception {
		EChartsModel echartsMoel = basicDataWapService.getDates(type,
				TimeType.getType(timeType), Long.valueOf(timeFrom), field);
		super.successData(FrameUtil.newHashMap("option", echartsMoel));
	}
}
